package com.yc.dao.task;

import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import btsm.bean.dbConnector;

import com.yc.dao.model.TaskBO;
import com.yc.util.ListContentsBFO;

public class TaskDAO {
	private static final Log log = LogFactory.getLog(TaskDAO.class);

	private dbConnector dbcon = null;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	private void dbConInit() {
		if (this.dbcon == null)
			this.dbcon = new dbConnector();
	}

	public void dbConClose() {
		if (this.dbcon != null)
			this.dbcon.closeAll();
		this.dbcon = null;
	}
	
	/**
	 * 创建任务
	 * @param bo
	 * @return
	 */
	public boolean createTask(TaskBO bo){
		boolean flag = true;
		try{
			dbConInit();
			StringBuffer sqlBuffer = new StringBuffer();
			sqlBuffer.append("insert into users(taskid, tasktype,acode,aname,lng,lat,alt,userid,rdtime,deadline,pmuid,status ) ");
			sqlBuffer.append("  values ( ");
			sqlBuffer.append("'" + bo.getTaskid() + "',");
			sqlBuffer.append("'" + bo.getTasktype() + "',");
			sqlBuffer.append("'" + bo.getAcode() + "',");
			sqlBuffer.append("'" + bo.getAname() + "',");
			sqlBuffer.append("'" + bo.getLng() + "',");
			sqlBuffer.append("'" + bo.getLat() + "',");
			sqlBuffer.append("'" + bo.getAlt() + "',");
			sqlBuffer.append("'" + bo.getUserid() + "',");
			sqlBuffer.append("'" + sdf.format(new Date()) + "',");
			sqlBuffer.append("'" + sdf.format(bo.getDeadline()) + "',");
			sqlBuffer.append("'" + bo.getPmuid() + "',");
			sqlBuffer.append("'" + bo.getStatus() + "')");
		 	log.info(sqlBuffer.toString());
			dbcon.execUpdate(sqlBuffer.toString());
		}catch(Exception e){
			flag = false;
			log.error("create task failed : ", e);
		}finally{
			dbConClose();
		}
		
		return flag;
	}
	
	/**
	 * 修改任务状态
	 * @param taskId
	 * @param status
	 * @return
	 */
	public boolean updateTaskStatus(String taskId,String status){
		boolean flag = true;
		try{
			 dbConInit();
			 StringBuffer sqlBuffer = new StringBuffer();
			 sqlBuffer.append(" update tasklst set status = '" + status + "' where taskid = '"+taskId+"' ");
			 log.info(sqlBuffer.toString());
			 dbcon.execUpdate(sqlBuffer.toString());
		}catch(Exception e){
			log.error("change task status failed : ", e);
			flag = false;
		}finally{
			dbConClose();
		}
		return flag;
	}
	
	public ListContentsBFO getTaskListByOwnerId(Map condition){
		dbConInit();
		ListContentsBFO bfo = new ListContentsBFO();
		List<TaskBO> list = new ArrayList<TaskBO>();
		int start = Integer.parseInt(condition.get("start").toString());
		int limit = Integer.parseInt(condition.get("limit").toString());
		ResultSet rs = null;
		try{
			String strSQL = " select count(taskid) from tasklst ";
			log.info(strSQL);
			rs = dbcon.execQuery(strSQL);
			if (rs.next()) {
				bfo.setTotalSize(rs.getInt(1));
			}
			strSQL = " select * from tasklst order by rdtime desc limit " + start + ", " + limit;
			log.info(strSQL);
			rs = dbcon.execQuery(strSQL);
			while (rs.next()) {
				TaskBO bo = new TaskBO();
				bo.setTaskid(rs.getString("taskid"));
				bo.setId(bo.getTaskid());
				bo.setTasktype(rs.getString("tasktype"));
				bo.setAcode(rs.getString("acode"));
				bo.setAname(rs.getString("aname"));
				bo.setLat(rs.getString("lat"));
				bo.setLng(rs.getString("lng"));
				bo.setAlt(rs.getString("alt"));
				bo.setMemo(rs.getString("memo"));
				bo.setUserid(rs.getString("userid"));
				bo.setPmuid(rs.getString("pmuid"));
				bo.setProceddtime(rs.getDate("proceddtime"));
				bo.setDeadline(rs.getDate("deadline"));
				bo.setStatus(rs.getInt("status"));
				bo.setRdtime(rs.getDate("rdtime"));
				list.add(bo);
			}
//			 bfo.setData(list.toArray());
		}catch(Exception e){
			 e.printStackTrace();
			log.error("query task failed : ", e);
		}finally{
			dbConClose();
		}
		 bfo.setData(list.toArray());
		return bfo;
	}
	
	public boolean deleteTask(String strTaskId){
		boolean flag = true;
		try{
			dbConInit();
			String strSQL = "delete from tasklst  where taskid = '" + strTaskId + "' ";
			log.info(strSQL);
			dbcon.execUpdate(strSQL);
		}catch(Exception e){
			log.error("delete task failed : ", e);
			flag = false;
		}finally{
			dbConClose();
		}
		return flag;
	}

}
